layui.define(['admin', 'layer', 'element', 'util'],function(exports){
    var $ = layui.$
    ,admin = layui.admin
    ,element = layui.element;

    MODEL:{

    };
    METHOD={
        init:function () {
            admin.req({
                url: '/getResource'
                ,type: 'get'
                ,done: function(res){
                    if(res.data){
                        for (var i = 0; i < res.data.length; i++) {
                            var menuOne = res.data[i];
                            var menu = "";
                            if(menuOne.list){
                                menu =
                                    "   <li data-name data-jump='"+menuOne.url+"' class='layui-nav-item'>" +
                                    "    <a href='javascript:;'>" +
                                    "       <i class='layui-icon "+menuOne.icon+"'></i>" +
                                    "       <cite>"+menuOne.name+"</cite>" +
                                    "    </a>" +
                                        METHOD.buildMenu(menuOne.list)+
                                    "  </li>"
                            }else{
                                menu =
                                    "   <li data-name data-jump='"+menuOne.url+"' class='layui-nav-item'>" +
                                    "    <a href='javascript:;' lay-href='"+menuOne.url+"'>" +
                                    "       <i class='layui-icon "+menuOne.icon+"'></i>" +
                                    "       <cite>"+menuOne.name+"</cite>" +
                                    "    </a>" +
                                    "  </li>"
                            }
                            $("#LAY-system-side-menu").append(menu);
                        }
                        element.init();
                    }
                }
            });


        },
        buildMenu:function (menuList) {
            if(menuList){
                var str = "<dl class='layui-nav-child'>";
                for (var i = 0; i <menuList.length; i++) {
                    var menuOne = menuList[i];
                    if(menuOne.list){
                        str += "<dd data-name data-jump='"+menuOne.url+"'>" +
                            // "       <a href='javascript:;' lay-href='"+menuOne.url+"'>" + menuOne.name + "</a>" +
                            "       <a href='javascript:;'>" +
                            "        <i class='layui-icon "+menuOne.icon+"'></i>" +
                            "        <cite>"+menuOne.name+"</cite>" +
                            "       </a>" +
                                    this.buildMenu(menuOne.list) +
                            "   </dd>";
                    }else{
                        str += "<dd data-name data-jump='"+menuOne.url+"'>" +
                            "<a href='javascript:;' lay-href='"+menuOne.url+"'>" +
                            "   <i class='layui-icon "+menuOne.icon+"'></i>" +
                            "       <cite>"+menuOne.name+"</cite>"+
                            "</a>" +
                            "</dd>";
                    }
                }
                str += "</dl>";
                return str;
            }
        }
    }

    METHOD.init();
    
    exports('layout', {})
})